package com.cddx.logistics.service.impl;

import com.cddx.logistics.domain.dto.QueryDTO;
import com.cddx.logistics.domain.dto.UserLoginDTO;
import com.cddx.logistics.domain.dto.UserRoleDTO;
import com.cddx.logistics.domain.pojo.Role;
import com.cddx.logistics.domain.pojo.User;
import com.cddx.logistics.domain.vo.UserVO;
import com.cddx.logistics.mapper.UserMapper;
import com.cddx.logistics.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean addUser(User user) {
        return userMapper.addUser(user) > 0;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user) > 0;
    }

    @Override
    public boolean deleteUser(Integer id) {
        return userMapper.deleteUser(id) > 0;
    }

    @Override
    public PageInfo<UserVO> queryPage(QueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<UserVO> list = userMapper.queryPage(queryDTO);
        return new PageInfo<>(list);
    }

    @Override
    public boolean addRole(UserRoleDTO userRoleDTO) {
        return userMapper.addRole(userRoleDTO) > 0;
    }

    @Override
    public boolean updateUserStatus(Integer id, Integer status) {
        return userMapper.updateUserStatus(id, status) > 0;
    }

    @Override
    public boolean deleteIds(Integer[] ids) {
        return userMapper.deleteIds(ids) > 0;
    }

    @Override
    public List<Role> queryRole(Integer id) {
        return userMapper.queryRole(id);
    }

    @Override
    public boolean deleteRole(UserRoleDTO userRoleDTO) {
        return userMapper.deleteRole(userRoleDTO) > 0;
    }

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        User user = userMapper.userInfo(userLoginDTO.getAccount());
        if(user != null){
            if(user.getPassword().equals(userLoginDTO.getPassword())){
                user.setPassword(null);
                return user;
            }
        }
        return null;
    }
    @Override
    public User userInfo(String account) {
        return userMapper.userInfo(account);
    }
}
